<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #div1{
            position: sticky;
            top: 0;
            height: 50px;
            line-height: 50px;
            text-align: center;
            background-color: #000;
            color: #fff;font-size: 18px;
        }
    </style>
</head>
<body>
    <div id="div1">首页</div>
    <div style="height:1000px;"></div>
    <div id="div2" style="height:100px;background:red;"></div>
    <script>
        var div2=document.getElementById('div2');
        var div1=document.getElementById('div1');
        let observe = new IntersectionObserver(
            function(entries){
                entries.forEach(function(element,index){
                    if(element.isIntersecting){
                        div1.innerText='红色方块出来了'
                    }
                    else{
                        div1.innerText='首页'
                    }
                })
            },
            {
                root:null,
                threshold:[0,1]
            }
        )
        observe.observe(div2)
    </script>
</body>
</html>